<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
</body>
<script>
    function ajax(options) { // {url, data, async = true, dataType = "text", success}

        let { type = "get", url, data, async = true, dataType = "text", success } = options;

        var xhr = new XMLHttpRequest();

        if (typeof data == "object") {  // 传入的数据是对象类型(条件不准确->后期补充)

            var list = [];
            for (var key in data) {
                var val = data[key];
                list.push(key + "=" + val)
            }
            data = list.join("&");
        }

        if (type.toLowerCase() == "get") { // GET get Get
            xhr.open("get", data ? url + "?" + data : url, async);
            xhr.send();
        } else if (type.toLowerCase() == "post") { // POST post Post
            xhr.open("post", url, async);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(data);
        }


        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;

                // 数据处理
                if (dataType == "json") {
                    result = JSON.parse(result);
                }
                // console.log(result);

                // 拿到数据之后的可能操作
                // 列表数据 =>  动态生成(成绩表 商品列表)
                // 数据增删改  => 增删改成功/失败


                if (success) { //  在请求成功之后调用回调函数
                    success(result);  // 回调函数调用过程中出入实际参数(请求的结果)
                }

            }
        }
    }


    ajax({
        // type:"get",
        url: "http://121.43.116.41/demo/php/searchGradeOrderLimit.php",
        // data: { wd: "", col: "id", type: "asc", page: 1, size: 10 },
        data: {
            wd: "",
            col: "id",
            type: "asc",
            page: 1,
            size: 10
        },
        async: true,
        dataType: "json",
        success: function (res) {
            console.log("成绩请求成功", res);
        }
    })


    ajax({
        type: "post",
        url: "http://121.43.116.41/demo/php/login_account.php",
        data: {
            account: "a123123",
            pwd: "123123",
        },
        async: true,
        dataType: "json",
        success: function (res) { // 请求成功时的回调函数 
            console.log("请求成功", res);

            var { status, message } = res;
            if (status) {
                console.log("登录成功")
            } else {
                console.log(message);
            }
        }
    })


</script>

</html>